// pages/info/info.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSelect:false,
    select:true,
    type:'',
    typec:'',
    choose:false,
    up:'/pic/三角上黑.png',
    down:'/pic/三角下黑.png',
    choosevalue:0,
    splitarray:[
      {index:0,type:'默认排序',display:true},
      {index:1,type:'最新',display:false},
      {index:2,type:'最热',display:false}],
    itemarray:[],
    personaltarget:true,
    wxAva:"",
    userName:"",
    academy:"",
    userClass:"",
    contactPhone:"",
    contactWx:"",
    contactQq:""
  },


  onLoad(options) {
    // console.log(options.itemtype)
    var that = this;
    var itemtype = options.itemtype;
    // 初始为默认排序
    this.setData({
      typec:this.data.splitarray[0].type,
      type:itemtype  
    })

  },
  onShow(){
    var that = this;

    // 未选综合
    var standard = "showAllItem";
    if(this.data.splitarray[0].display){
      standard = "showAllItem"
    }else if(this.data.splitarray[1].display){
      standard = "selectItemByNew"
    }else if(this.data.splitarray[2].display){
      standard = "showAllItem"
    }
    // 选择销量
    if(this.data.choose){
      standard = "selectItemByHot"
    }
    // 价格从高到底
    if(this.data.choosevalue == 1){
      standard = "selectItemByUp"
    }
    if(this.data.choosevalue == 2){
      standard = "selectItemByDown"
    }

    wx.request({
      url: 'http://127.0.0.1:8080/item/'+standard,
      method:"POST",
      data:{
        itemtype:that.data.type
      },
      success(res){
        // console.log(res)
        var iteminfo = res.data
        for(var i=0;i<iteminfo.length;i++){
          iteminfo[i].itemPic = JSON.parse(iteminfo[i].itemPic)
        }
        that.setData({
          itemarray:iteminfo
        })
      }
    }) 
  },
  /*改变种类 */
  changetype(e){
    var value  = this.data.isSelect;
    this.setData({
      choose:false,
      choosevalue:0,
      up:'/pic/三角上黑.png',
      down:'/pic/三角下黑.png',
      isSelect:!value,
      select:true
    })
  },
  /*选择种类 */
  choosetype(e){
    var index = e.currentTarget.dataset.index;
    
    // console.log(index)
    var that = this;
    var standard = ""
    if(index == 1){
      standard = "selectItemByNew";
    }else if(index == 2){
      standard ="showAllItem";
    }else{
      standard ="showAllItem";
    }
    wx.request({
      url: 'http://127.0.0.1:8080/item/'+standard,
      method:"POST",
      data:{
        itemtype:that.data.type
      },
      success(res){
        // console.log(res)
        var iteminfo = res.data
        for(var i=0;i<iteminfo.length;i++){
          iteminfo[i].itemPic = JSON.parse(iteminfo[i].itemPic)
        }
        that.setData({
          itemarray:iteminfo
        })
      }
    })


    for(var i=0 ;i<3;i++){
      var value = 'splitarray['+i+'].display';
      if(i == index){
        this.setData({
          [value]:true,
          isSelect:false,
          typec:this.data.splitarray[i].type
        })
      }
        else{
          this.setData({
            [value]:false
          })
        }
    }
    
  },
  /*选择高销量 */
  choosesale(){
      this.setData({
        select:false,
        choose:true,
        choosevalue:0,
        up:'/pic/三角上黑.png',
        down:'/pic/三角下黑.png',
        isSelect:false
      })
      var that = this;
      wx.request({
        url: 'http://127.0.0.1:8080/item/selectItemByHot',
        method:"POST",
        data:{
          itemtype:that.data.type
        },
        success(res){
          // console.log(res)
          var iteminfo = res.data
          for(var i=0;i<iteminfo.length;i++){
            iteminfo[i].itemPic = JSON.parse(iteminfo[i].itemPic)
          }
          that.setData({
            itemarray:iteminfo
          })
        }
      })
  },
  /*价格升序降序 */
  choosevalue(){
    var that = this;
    var value = this.data.choosevalue
    if(value == 0){
      // console.log(value)
      wx.request({
        url: 'http://127.0.0.1:8080/item/selectItemByUp',
        method:"POST",
        data:{
          itemtype:that.data.type
        },
        success(res){
          // console.log(res)
          var iteminfo = res.data
          for(var i=0;i<iteminfo.length;i++){
            iteminfo[i].itemPic = JSON.parse(iteminfo[i].itemPic)
          }
          that.setData({
            itemarray:iteminfo
          })
        }
      })
      this.setData({
        up:'/pic/三角上.png',
        choosevalue:1
      })
    }else if(value == 1){

      wx.request({
        url: 'http://127.0.0.1:8080/item/selectItemByDown',
        method:"POST",
        data:{
          itemtype:that.data.type
        },
        success(res){
          // console.log(res)
          var iteminfo = res.data
          for(var i=0;i<iteminfo.length;i++){
            iteminfo[i].itemPic = JSON.parse(iteminfo[i].itemPic)
          }
          that.setData({
            itemarray:iteminfo
          })
        }
      })
      // console.log(value)
      this.setData({
        down:'/pic/三角下.png',
        up:'/pic/三角上黑.png',
        choosevalue:2
      })
    }else{
      // console.log(value)
      wx.request({
        url: 'http://127.0.0.1:8080/item/selectItemByUp',
        method:"POST",
        data:{
          itemtype:that.data.type
        },
        success(res){
          // console.log(res)
          var iteminfo = res.data
          for(var i=0;i<iteminfo.length;i++){
            iteminfo[i].itemPic = JSON.parse(iteminfo[i].itemPic)
          }
          that.setData({
            itemarray:iteminfo
          })
        }
      })
      this.setData({
        up:'/pic/三角上.png',
        down:'/pic/三角下黑.png',
        choosevalue:1
      })
    }
    var value  = this.data.isSelect;
    this.setData({
      choose:false,
      select:false,
      isSelect:false
    })
    
  },
  /*选择物品 */
  chooseitem(e){
    // console.log(e)
    var index = e.currentTarget.dataset.index;
    var itemid = this.data.itemarray[index].itemID;
    wx.navigateTo({
      url: '/pages/iteminfo/iteminfo?itemid='+itemid,
    })
  },

  showperson(e){
    var index = e.currentTarget.dataset.index;
    var iteminfo = this.data.itemarray[index];
    this.setData({
      wxAva:iteminfo.wxAva,
      userName:iteminfo.userName,
      academy:iteminfo.academy,
      userClass:iteminfo.userClass,
      contactPhone:iteminfo.contactPhone,
      contactWx:iteminfo.contactWx,
      contactQq:iteminfo.contactQq,
      personaltarget:false
    })
  },
  cancelperson(){
    this.setData({
      personaltarget:true
    })
  }
})